<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\index\model\Set;
use app\index\model\Game;
use app\index\model\Pool;
use app\index\model\User;
use app\index\model\Log;
use think\Request;
class Tb extends Base{
	/**
	 * 获取首页信息
	 *
	 * @return void
	 * @author 
	 **/
	public function index()
	{
		$mini_app_id=Request::instance()->param('mini_app_id');
		$set=Set::get(['mini_app_id'=>$mini_app_id]);
		$person_num=User::where('mini_app_id',$mini_app_id)->count();//获取参与人数
		$max_limit=$set->max_limit;
		$percent=round($person_num/$max_limit);//参与人数百分比
		$start_time=$set->duration[0];
		$end_time=$set->duration[1];
		$start_time=strtotime($start_time);//开始日期转换为时间戳
		$end_time=strtotime($end_time);//结束日期转换为时间戳
		if(time()<$start_time){
			$status=0;
			$timeStamp=$start_time;
		}elseif(time()>$start_time&&time()<$end_time){
			$status=1;
			$timeStamp=$end_time;
		}else{
			$status=2;
		}
		$pool=Pool::get(['mini_app_id'=>$mini_app_id]);
		$set['person_num']=$person_num;
		$set['percent']=$percent;
		$set['status']=$status;
		$set['timeStamp']=$timeStamp;
		$set['pool']=$pool;
		$this->success($set);

	}
	/**
	 * 增加页面浏览次数
	 *
	 * @return void
	 * @author 
	 **/
	public function addPageView()
	{
		$mini_app_id=Request::instance()->param('mini_app_id');
		$set=Set::get(['mini_app_id'=>$mini_app_id]);
		$set->page_view+=1;
		$set->save();

	}
	/**
	 * 获取礼盒进度
	 *
	 * @return void
	 * @author 
	 **/
	public function my()
	{
		$open_id=Request::instance()->param('open_id');
		$mini_app_id=Request::instance()->param('mini_app_id');
		$game=Game::get(['mini_app_id'=>$mini_app_id]);
		$shareNumber=$game->shareNumber;//获取游戏规则中的分享次数
		$number1=Log::where(['mini_app_id'=>$mini_app_id,'open_id'=>$open_id])->count();//获取拆盒子的人数
		if($number1<$shareNumber){
			$nunmber2=$shareNumber-$number1;//还需几个人拆盒子
			$percent=round($number1/$shareNumber);//拆盒子进度
			$data=[
			'number1'=>$number1,
			'number2'=>$number2,
			'percent'=>$percent,
			'status'=>0];
		}else{
			$data=[
			'number1'=>$number1,
			'number2'=>0,
			'percent'=>100,
			'status'=>1];
		}
		
		$this->success($data);

	}

}